package cn.itcast.n4;

import lombok.extern.slf4j.Slf4j;

import static cn.itcast.n2.util.Sleeper.sleep;

@Slf4j(topic = "c.TestCorrectPosture")
public class TestCorrectPostureStep1 {

    static final Object room = new Object();
    static boolean hasCigarette = false;
    static boolean hasTakeout = false;

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (room){
                log.debug("有烟没？[{}]", hasCigarette);
                while (!hasCigarette) {
                    log.debug("没烟，先歇会儿！");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                log.debug("有烟没？[{}]",hasCigarette);
                if (hasCigarette) {
                    log.debug("可以干活了");
                }
            }
        },"小南").start();

        new Thread(() -> {
            synchronized (room){
                log.debug("外卖送到没？[{}]", hasTakeout);
                while (!hasTakeout) {
                    log.debug("没外卖，先歇会儿！");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                log.debug("外卖送到没？[{}]",hasTakeout);
                if (hasTakeout) {
                    log.debug("可以开始干活了");
                }else {
                    log.debug("没饭，干不成活...");
                }
            }
        },"小女").start();

        sleep(1);
        new Thread(() -> {
            synchronized (room){
                hasTakeout = true;
                log.debug("外卖送到了。");
                room.notify();//随机唤醒，虚假唤醒
//                room.notifyAll();
            }
        },"送外卖").start();
    }

    private static void stem3() {
        new Thread(() -> {
            synchronized (room){
                log.debug("有烟没？[{}]", hasCigarette);
                if (!hasCigarette) {
                    log.debug("没烟，先歇会儿！");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                log.debug("有烟没？[{}]",hasCigarette);
                if (hasCigarette) {
                    log.debug("可以干活了");
                }
            }
        },"小南").start();

        new Thread(() -> {
            synchronized (room){
                log.debug("外卖送到没？[{}]", hasTakeout);
                if (!hasTakeout) {
                    log.debug("没外卖，先歇会儿！");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                log.debug("外卖送到没？[{}]",hasTakeout);
                if (hasTakeout) {
                    log.debug("可以开始干活了");
                }else {
                    log.debug("没饭，干不成活...");
                }
            }
        },"小女").start();

        sleep(1);
        new Thread(() -> {
            synchronized (room){
                hasTakeout = true;
                log.debug("外卖送到了。");
//                room.notify();//随机唤醒，虚假唤醒
                room.notifyAll();//随机唤醒，虚假唤醒
            }
        },"送外卖").start();
    }

    private static void step2() {
        new Thread(() -> {
            synchronized (room){
                log.debug("有烟没？[{}]", hasCigarette);
                if (!hasCigarette) {
                    log.debug("没烟，先歇会儿！");
                    try {
                        room.wait(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                log.debug("有烟没？[{}]",hasCigarette);
                if (hasCigarette) {
                    log.debug("可以干活了");
                }
            }
        },"小南").start();

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                synchronized (room){
                    log.debug("可以干活了");
                }
            },"其他人"+i).start();
        }

        sleep(1);
        new Thread(() -> {
            synchronized (room){
                hasCigarette =true;
                log.debug("烟送到了。");
                room.notify();
            }
        },"送烟的").start();
    }

    private static void step1() {
        new Thread(() -> {
            synchronized (room){
                log.debug("有烟没？[{}]", hasCigarette);
                if (!hasCigarette) {
                    log.debug("没烟，先歇会儿！");
                    sleep(2);
                }

                log.debug("有烟没？[{}]",hasCigarette);
                if (hasCigarette) {
                    log.debug("可以干活了");
                }
            }
        },"小南").start();

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                synchronized (room){
                    log.debug("可以干活了");
                }
            },"其他人"+i).start();
        }

        sleep(1);
        new Thread(() -> {
            synchronized (room){
                hasCigarette =true;
                log.debug("烟送到了。");
            }
        },"送烟的").start();
    }
}
